﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Component;

public partial class BackEnd_CategorySub : System.Web.UI.UserControl
{
    public string _urlChung = "";
    List<CategoryInfo> listC = new List<CategoryInfo>();
    List<NewsInfo> listI = new List<NewsInfo>();
    public string _urlCTGRL = BackEndQuery._AdminQuery + BackEndQuery._CategoryL;
    public string _urlCTGRL_TT = BackEndQuery._AdminQuery + BackEndQuery._CategorySub + "&" + ConstString._Parent + ConstString._dauBang;
    public string _Url = BackEndQuery._AdminQuery + BackEndQuery._CategoryM;
    public string _UrlModify = BackEndQuery._AdminQuery + BackEndQuery._CategoryM + "&" + ConstString._idcat + ConstString._dauBang;
    public string _UrlModifyShow = BackEndQuery._AdminQuery + BackEndQuery._CategoryMS + "&" + ConstString._idcat + ConstString._dauBang;
    string _UrlCateSub = BackEndQuery._AdminQuery + BackEndQuery._CategorySub + "&" + ConstString._Parent + ConstString._dauBang;
    string _UrlNewsL = BackEndQuery._AdminQuery + BackEndQuery._NewsL + "&" + ConstString._Parent + ConstString._dauBang;
    public string _Session = "";
    string _panrent = "";
    public string _rPanrent = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        _urlChung = "<a href=\"" + _urlCTGRL + "\" >Danh mục</a>" + " -> " + "<a href=\"" + _urlCTGRL_TT + "6" + "\">Tin Tức" + "</a>";
        _panrent = Request.QueryString[ConstString._Parent] ?? "";
        _rPanrent = Request.QueryString[ConstString._RParent] ?? "";
        if (_panrent == "")
            _panrent = _rPanrent;
        Load_Data();
        Session["cat"] = _Session = "<a href=\"" + Request.RawUrl + "\" >Danh mục</a>";
        grid.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
        grid.RowCommand += new GridViewCommandEventHandler(grid_RowCommand);
    }
    
    void grid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "lbtUpdate")
        {
            Response.Redirect(_UrlModifyShow + e.CommandArgument.ToString());
        }
        if (e.CommandName == "lbtCategorySub")
        {
            listC = CategoryDB.GetByParent(ConvertType.ToInt(e.CommandArgument));
            listI = NewsDB.GetByParent(ConvertType.ToInt(e.CommandArgument));
            if (listC.Count == 0 && listI.Count != 0)// && listI != null
            {
                Response.Redirect(_UrlNewsL + e.CommandArgument.ToString());
            }
            Response.Redirect(_UrlCateSub + e.CommandArgument.ToString());
        }

    }

    private void Load_Data()
    {
        grid.DataKeyNames = new string[] { ColumCategory._Id };
        grid.DataSource = CategoryDB.GetByParent(int.Parse(_panrent == "" ? "0" : _panrent));
        grid.DataBind();
    }

    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        TextBox txtSummary = (TextBox)grid.Rows[e.RowIndex].FindControl("txtSummary");
        TextBox txtName = (TextBox)grid.Rows[e.RowIndex].FindControl("txtName");
        TextBox txtPriority = (TextBox)grid.Rows[e.RowIndex].FindControl("txtPriority");
        CategoryInfo cat = CategoryDB.GetById((int)id);
        cat.Summary = Convert.ToString(txtSummary.Text);
        cat.Name = Convert.ToString(txtName.Text);
        cat.Priority = Convert.ToInt32(txtPriority.Text);
        Response.Redirect(Request.RawUrl);
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        Load_Data();
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            CategoryInfo cat = CategoryDB.GetById(ConvertType.ToInt(id));
            if (CategoryDB.Delete((int)id))
                this.labMessage.Text = ConstString._xoaTC;
            else
                labMessage.Text = ConstString._xoaTB;
        }
        catch { }
        Load_Data();
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        Load_Data();
    }
    protected void click(object sender, EventArgs e)
    {
       
    }
}
